﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Data;

public partial class admin_addCourse : System.Web.UI.Page
{
    db_course course = new db_course();
    protected void Page_Load(object sender, EventArgs e)
    {
        Server.Execute("../inc/checkPurview.aspx");  // 此处用户权限验证
        if (!IsPostBack)
        {
        	initCourseId();
        	initCourseType();
        	initTeacherId();

            string id = Request.QueryString["id"];
    		if (!string.IsNullOrEmpty(id))
    		{
    			ddl_teacherId.SelectedValue = Request.QueryString["id"];
    		}
        }
    }

		private void initCourseId()
		{
			string courseId = course.getNewCourseId();
			tb_courseId.Text = courseId;
		}
		
		private void initCourseType()
		{
			DataSet ds = new DataSet();
			ds = course.getCourseType();
			if (ds.Tables.Count != 0)
			{
				ddl_courseType.DataSource = ds.Tables[0].DefaultView;
				ddl_courseType.DataTextField = "typeName";
				ddl_courseType.DataValueField = "typeId";
				ddl_courseType.DataBind();
				ddl_courseType.Items.Insert(0, new ListItem("请选择课程类型", ""));
				ddl_courseType.Items[0].Selected = true;
			}
		}

        private void initTeacherId()
        {
            db_teacher teacher = new db_teacher();
            DataSet ds = new DataSet();

            ds = teacher.selectTeacherALL("0");

            if (ds.Tables.Count != 0)
            {
                ddl_teacherId.DataSource = ds.Tables[0].DefaultView;
                ddl_teacherId.DataTextField = "teacherName";
                ddl_teacherId.DataValueField = "teacherId";
                ddl_teacherId.DataBind();
                ddl_teacherId.Items.Insert(0, new ListItem("请选择任课教师", ""));
                ddl_teacherId.Items[0].Selected = true;
            }
        }

    protected void btn_res_Click(object sender, EventArgs e)
    {
       // ddl_courseType.Items[0].Selected = true;
        tb_about.Text = "";
        tb_courseHour.Text = "";
       // ddl_teacherId.Items[0].Selected = true;
        tb_gold.Text = "";
    }
    protected void btn_ok_Click(object sender, EventArgs e)
    {
        string courseId = tb_courseId.Text;
        string courseType = ddl_courseType.SelectedValue;
        string about = tb_about.Text;
        string courseHour = tb_courseHour.Text;
        string teacherId = ddl_teacherId.SelectedValue;
        string gold = tb_gold.Text;
        string courseName = tb_courseName.Text;
        try
        {
            float.Parse(courseHour);
        }
        catch
        {
            Response.Write("<script language=javascript>alert('输入正确的课时！');</script>");
            tb_courseHour.Focus();
            return;
        }
        try
        {
            float.Parse(gold);
        }
        catch
        {
            Response.Write("<script language=javascript>alert('输入正确的售价！');</script>");
            tb_gold.Focus();
            return;
        }
        if (course.addCourse(courseId,courseType,courseHour,about,teacherId,gold,courseName))
        {
            Response.Write("<script language=javascript>alert('新增课程成功！');location.href='manageCourse.aspx';</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('失败！');</script>");
        }
    }
}